package main

import (
	"fmt"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jinzhu/gorm"
	"go-demo/models"
)

func main() {
	db, connectErr := gorm.Open("mysql", "root:root@/demo?charset=utf8&parseTime=True&loc=Local")
	defer func() {
		println("defer...")
		if err := recover(); err != nil {
			fmt.Println("connect Err：", err)
		}

	}()
	if connectErr != nil {
		panic(connectErr)
	}
	first(db)
	if err := db.Close(); err != nil {
		fmt.Println("close Err：", err)
	}
}

func first(db *gorm.DB) {
	user := &models.Users{}
	//db.First(user)
	//db.First(user,2)
	//db.Where("id =2").First(user)
	//db.Where("id in(?) and id >? ", []int{2}, 0).First(user)
	//db.Where(map[string]interface{}{"name": "名称2", "status": 1}).First(user)
	//db.Where(&models.Users{Name: "名称2"}).First(user)
	db.First(user, &models.Users{Name: "名称2"})
	fmt.Println(user)
	fmt.Println(user)
}

// 创建
func create(db *gorm.DB) {
	user := models.Users{Name: "名称2", Pwd: "密码2", Status: true}
	db.Create(&user)
	fmt.Println(user)
}
